<template>
    <div id="id_hbw">
    <div class="search_box">
        <input type="text" v-model="bizNo" />
        <button @click="search">查询</button>
    </div>
       <table>
        <thead>
            <th>Id</th>
            <th>请求时间</th>
            <th>请求类型</th>
            <th>###</th>
        </thead>
        <tbody>
            <tr v-for="it in tableData">
                <td>{{ it.id }}</td>
                <td>{{ it.time }}</td>
                <td>{{ it.type }}</td>
                <td>
                   <table>
                   <thead>
                    <th>订单编码</th>
                    <th>是否拼单</th>
                    <th>支付类型</th>           
                    <th>预估费用</th>
                    <th>计算费用</th>
                   </thead>
                   <tbody>
                    <tr v-for="c in it.infos">
                        <td>{{ c.number }}</td>
                        <td>{{ c.isPd }}</td>
                        <td>{{ c.payType }}</td>                       
                        <td>{{ c.yg }}</td>
                        <td>{{ c.amount }}</td>
                    </tr>
                    </tbody>
                   </table> 
                </td>
            </tr>
        </tbody>
       </table>
    </div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from 'vue'

let tableData = ref<any>([])
let bizNo = ref<string>('HPWCP2023011808574777858')

let postSql = (sql: string) => {
    let request = {
        "driver": "com.mysql.cj.jdbc.Driver",
        "url": "jdbc:mysql://rm-2vc7hmlv7q5y03r5g.mysql.cn-chengdu.rds.aliyuncs.com:3306/bms_prd?createDatabaseIfNotExist=true&useServerPrepStmts=false&rewriteBatchedStatements=true&characterEncoding=UTF-8",
        "user": "root",
        "password": "vCXGFDubW8srqeOi",
        "sql": sql
    }
    return fetch('https://bms-webapi.haipawang.cn/demo/sql/exe', {
        method: 'post',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(request)
    }).then(res => res.json())
    .then(data => data.result)
}

let search = () => {    
    tableData.value = []
    let rsData = postSql("select * from user_event_log where body like '%"+bizNo.value+"%' order by Id limit 100")
   rsData.then(result => {
        for(let i in result) {
            let item = result[i]
            let request = JSON.parse(item.Body)[0]
            let response = JSON.parse(item.ResultBody)
            let infos:any[] = []
            if (request.businessDataTypeCode == 'ST_HBW_OMS') {
                for(let j in request.items) {
                    let omsItem = request.items[j]
                    let amount = 0
                    let res = response.caculateResult.filter(x => x.businessNumber ==  omsItem.businessNumber)
                    if (res && res.length > 0) {
                        amount = res[0].amount
                    }
                    infos.push({
                        number: omsItem.businessNumber,
                        payType: omsItem.extensionData['支付类型'],
                        kg: omsItem.extensionData['商品总重量'],
                        js: omsItem.extensionData['商品总件数'],
                        amount
                    })
                }                
            } else {
                for(let j in request.items) {
                    let tmsItem = request.items[j]
                    for (let tms_i in tmsItem.extensionData['运单详情']) {
                        let orderItem = tmsItem.extensionData['运单详情'][tms_i]
                        let amount = 0
                        let res = response.caculateResult.filter(x => x.businessNumber ==  orderItem['订单编码'])
                        if (res && res.length > 0) {
                            amount = res[0].amount
                        }
                        infos.push({
                            number: orderItem['订单编码'],
                            payType: orderItem['支付类型'],
                            kg: orderItem['订单编码'],
                            js: orderItem['订单编码'],
                            isPd: orderItem['是否拼单'],
                            yg: orderItem['预估费用'],
                            amount
                        })
                    }                    
                }            
            }            
            tableData.value.push({
                id: item.Id,
                time: item.EventTime,
                type: request.businessDataTypeCode,
                infos                
            })
        }
   })
}

onMounted(() => {
    search();
})

</script>

<style lang="scss" scoped>
#id_hbw {
    height: 100vh;
    overflow: auto;
    .search_box {
        padding: 1em;
        margin: 10px;
        input {
            margin: 0 1em;
            width: 600px;
        }
    }
}
 table, table tr td { padding: 0.5em 1em; border-bottom:1px solid #ccc; text-align: left; }
    table thead th {
        padding: 0.5em 1em; border-bottom:2px solid #ccc; text-align: left;
    }
    table { width: 100%; min-height: 25px; line-height: 25px; border-collapse: collapse;}    
</style>